<?php
/**
 * Created by PhpStorm.
 * User: liugang
 * Date: 2019-03-30
 * Time: 15:48
 */

namespace App\Library;


class Time
{
    /**
     * 将时间戳转换为多少秒|分钟|小时|天前
     *
     * @param $timestamp
     * @return string
     */
    public static function swtichTimeStamp($timestamp)
    {
        if (!$timestamp) {
            return '';
        }

        $s = (int)time() - $timestamp;

        if ((0 < $s) && ($s < 60)) {
            return $s . '秒前';
        } elseif ((60 < $s) && ($s < 3600)) {
            $mins = $s / 60;
            return floor($mins) . '分钟前';
        } elseif ((3600 < $s) && ($s < 86400)) {
            $hours = $s / 3600;
            return floor($hours) . '小时前';
        } elseif ($s >= 86400) {
            $days = $s / 86400;
            return floor($days) . '天前';
        }

        return '';
    }
}